import {useDispatch} from "react-redux";
import store from "@/store";
import {useMemo} from "react";
import {renderMenuItem} from "@/routes";
import {matchPath, useLocation} from "react-router-dom";
type TAuth = typeof  store.dispatch
export const useAppDispatch = ()=>useDispatch<TAuth>()

export const useAppTitle = ()=>{
    const location = useLocation()
    return  useMemo(()=>{
        //找到路由的path
        const path = "/"+location.pathname.split("/")[1]
        //根据路由path找到路由信息
        const info = renderMenuItem().find((v:any)=>v.path === path);
        //找到一级标题
        const title = info.label;
        //找二级标题
        let subTitle;
        if(info.children){
            //通过match方法判断浏览器的地址和路由的路径是否一致
            const subInfo = info.children.find((v:any)=> matchPath(v.path,location.pathname))
            if(subInfo) subTitle = subInfo.label
        }
        return {
            title,
            subTitle
        }
    },[location.pathname])}
